--- title: Static Plots keywords: fastai sidebar: home_sidebar summary: "All types of plots in matplotlib are included in this module. This includes bands,DOS and other general plots, which can be saved as pdf,png,jpg and other formats that matplotlib offers." description: "All types of plots in matplotlib are included in this module. This includes bands,DOS and other general plots, which can be saved as pdf,png,jpg and other formats that matplotlib offers." nb_path: "StaticPlots.ipynb" ---
plot_bands. It requires 10 arguments and most of them are default, but in order to tweak parameters, you still need to access them. Follow These steps to input arguments easily.plot_bands? and hit enter. This will give Signature and DocString.dict(what you copied). In a Jupyter Notebook cell, you can edit it:arg_dict=dict(
ax=None,
kpath=None,
bands=None,
showlegend=True,
E_Fermi=0,
color1=(1, 0, 0.8),
style1='solid',
lw1=0.7,
)
arg_dict
** inside function, it will pass all arguments present in dictionary. Make sure you do not change name of variables, although you can delete as few of them.plot_bands(**arg_dict). You can edit dictionary on fly, or can save it to a file to read (Not recommended, could be a threat to your system security as reading dictionaries from a file could run potentially harmful commands as well.)Working in object-oriented way, we can have plenty of options in matplotlib. See the example below, which provides an overview of flexibility of matplotlib. All functions are defined in object-oriented way for better compatibility and flexibility.
import pivotpy.vr_parser as vp
vr1=vp.export_vasprun(path=f1)
vr2=vp.export_vasprun(path=f2)
import pivotpy.s_plots as sp
import matplotlib.pyplot as plt
fig,ax=plt.subplots(1,3,figsize=(10.2,2.6),sharey=True)
ax0=sp.plot_bands(ax=ax[0],kpath=vr2.kpath,bands=vr2.bands,showlegend=True)
ax1=sp.plot_bands(ax=ax[1],kpath=vr1.kpath,bands=vr1.bands,color1='y')
ax2=sp.plot_bands(ax=ax[2],kpath=vr1.kpath,bands=vr1.bands,color1='y')
ax2=sp.plot_bands(ax=ax[2],kpath=vr2.kpath,bands=vr2.bands,showlegend=True)
xticks=[vr1.kpath[i] for i in [0,30,60,-1]]
txts=["Polarized","Unpolarized","Comparison"]
for axes,txt in zip(ax,txts):
if axes==ax0:
sp.modify_axes(ax=axes,ylabel='Energy (eV)')
sp.modify_axes(ax=axes,ylim=[-10,10],xlim=[xticks[0],xticks[-1]],xticks=xticks,xt_labels=[r'$\Gamma$','M','K',r'$\Gamma$'])
axes.text(0.05,0.9,txt,bbox=dict(edgecolor='white',facecolor='white', alpha=0.9),transform=axes.transAxes,color='red')
plt.subplots_adjust(hspace=0.01,wspace=0.05)
Below is example where you can add multiple text entries on a quick_bplot.
ax=sp.quick_bplot(path_evr=f2,elim=[-5,5],xt_indices=[0,30,60,-1],xt_labels=['W','K',''],txt='Graphene',ctxt='r')
sp.add_text(ax=ax,xs=[0.35,0.5],ys=[0.55,0.7],txts=[r'$E_{gap}$ = 0 eV','edge states'],colors=['red','blue'])
import pivotpy.s_plots as sp
import matplotlib.pyplot as plt
plt.style.use('default')
fig,ax=plt.subplots(1,2,figsize=(9,1))
add_colorbar(ax[0],'RGB',vertical=True,N=7)
add_colorbar(ax=ax[1],cmap_or_clist=['r','g','b',2],N=21)
create_rgb_line in object oriented way. max_width, so you can compare, say, s line and p line with each other.import time
start=time.time()
import numpy as np
import pivotpy.vr_parser as vp
path='E:/Research/graphene_example/ISPIN_2/bands/vasprun.xml'
vr=vp.export_vasprun(path=path,elim=[-10,10])
k=vr.kpath
ef=vr.tdos.E_Fermi
en=vr.bands.evals.SpinUp-ef
pros=vr.pro_bands.pros.SpinUp
import pivotpy.s_plots as sp
import matplotlib.pyplot as plt
fig1,ax = plt.subplots(1,3,figsize=(10.2,2.6),sharey=True)
axc = fig1.add_axes([0.5,1,0.3,0.04])
C1 = range(0,1)
spd = [[0],[1],[2,3]]
sp.create_rgb_lines(ax=ax[0],kpath=k,evals_set=en,pros_set=pros,interpolate=True,n=15,uni_color=True,orbs=[[0],[0],[0]],ions=C1)
sp.create_rgb_lines(ax=ax[0],kpath=k,evals_set=en,pros_set=pros,\
uni_color=True,color=(0,1,0,1),orbs=[[],[1],[]],ions=C1)
sp.create_rgb_lines(ax=ax[0],kpath=k,evals_set=en,pros_set=pros,uni_color=True,color=(0,0,1,0.4),orbs=[[],[],[2,3]],ions=C1)
sp.create_rgb_lines(ax=ax[1],kpath=k,evals_set=en,pros_set=pros,orbs=spd,ions=C1)
sp.create_rgb_lines(ax=ax[2],kpath=k,evals_set=en,pros_set=pros,\
uni_width=True,orbs=spd,ions=C1)
[sp.modify_axes(ax=axes) for axes in ax]
sp.add_legend(ax=ax[0],labels=['C-s','C-pz','C-px+py'],widths=4)
sp.add_colorbar(ax=axc,colors=[(1,0,0),(0,1,0),(0,0,1)],ticklabels=['C-s','C-pz','C-px+py'],ticks=[0,0.5,1])
plt.subplots_adjust(wspace=0.05)
txts=['uni_color=True','variable width/color','uni_width=True']
[sp.add_text(ax=axes,txts=txt) for axes,txt in zip(ax,txts)];
print('Executed in {} seconds.'.format(time.time()-start))
create_rgb_lines(uni_color=False). You can pass lists for orbs,labels,colors and elements eeach of length 3 with one axis. elements argument is special, you can pass index of element which will pick all ions of that type, or list(length=3) of indices of ions, e.g in elements=[0,[0,1],2] of system Ga32As31Bi1, 0 and 2 pick all ions of Ga and Bi respectively, while [0,1] will pick first two ions of Ga.scale_color=True, ecah point on plot is scaled to maximum color, if False, whole plot is scaled.color_matrix is to convert between color spaces and play around as you can. Sum of each row should be less than or equal to 1. Only works if scale_color=True.
{% include tip.html content='Gray scale matrix = pivotpy.gray_matrix and kind of body centered cubic matrix = pivotpy.color_matrix.' %}{% include tip.html content='Use RGB_m for DOS and RGB_f for bands plotting in same color. RGB_f is auto-picked by add_colorbar.' %}import os
import numpy as np
os.chdir('E:/Research/graphene_example/ISPIN_2/bands')
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import pivotpy as pp
evr = pp.export_vasprun()
args_dict = dict(path_evr=evr,elements=[0,[0,1],[0,1]],orbs=[0,[1],[2,3]],labels=['s','p$_z$','p$_x$+p$_y$'])
axs=pp.init_figure(nrows=2,ncols=3,figsize=(9,5),sharex=True,sharey=True);
caxs = pp.init_figure(nrows=2,ncols=3,figsize=(9,0.5),sharex=True,sharey=True)
quick_rgb_lines(ax=axs[0,0],**args_dict,spin='up',txt='scale_color=False',scale_color=False)
add_colorbar(caxs[0,0])
quick_rgb_lines(ax=axs[0,1],**args_dict,spin='down',txt='Intensity reduced',color_matrix=np.diag([0.5,0.7,0.5]))
add_colorbar(caxs[0,1])
quick_rgb_lines(ax=axs[0,2],**args_dict,spin='both',txt='scale_color=True')
add_colorbar(caxs[0,2])
plt.style.use(['default','seaborn'])
_ = quick_rgb_lines(ax=axs[1,0],**args_dict,spin='up',txt='color_matrix',color_matrix=pp.color_matrix)
add_colorbar(caxs[1,0])
_ = quick_rgb_lines(ax=axs[1,1],**args_dict,spin='down',txt='color_matrix.T',color_matrix=pp.color_matrix.T)
add_colorbar(caxs[1,1])
_ = quick_rgb_lines(ax=axs[1,2],**args_dict,spin='both',txt='gray_matrix',color_matrix=pp.gray_matrix)
add_colorbar(caxs[1,2])
create_rgb_lines(uni_color=True,scale_color=False). You can pass equal length lists for orbs,labels, and elements either with one axis or mutltiple axes. elements argument is special, you can pass index of element which will pick all ions of that type, or list(length=3) of indices of ions, e.g in elements=[0,[0,1],2] of system Ga32As31Bi1, 0 and 2 pick all ions of Ga and Bi respectively, while [0,1] will pick first two ions of Ga.len(axis)=1, all projections are plotted on single axis and you can tweak opacity, legend display etc. There are plenty of options.Instead of giving custom colors, you can use matplotlib's colormaps to be consistent. Use
plt.colormaps()
to see list of available color maps. To get a color array from a map, you can do the following:
from matplotlib.pyplot import cm
colors = cm.hsv(np.linspace(0,1,3))
# This will give you three colors from 'hsv' map.
Note: Two custom colormaps RGB,RGB_m are registered in session when you import pivotpy,which could be used when plotting DOS with bands of same color. RGB_m at any moment would represent the latest quick_rgb_lines.
import os
path='E:/Research/graphene_example/ISPIN_2/bands'
os.chdir(path)
import pivotpy as pp
import matplotlib.pyplot as plt
axs=pp.init_figure(nrows=1,ncols=3,figsize=(7,2.5),sharey=True,sharex=True)
args_dict=dict(elements=[0,0,[0,1]],orbs=[0,1,[2]],labels=['s','$p_z$','$p_x$'],hspace=0.1,wspace=0.07,showlegend=True)
quick_color_lines(axes=axs[0],**args_dict,left=0.06,color_map='flag',spin='up');
quick_color_lines(axes=axs[1],**args_dict,left=0.06,color_map='FCC',spin='down');
quick_color_lines(axes=axs[2],**args_dict,left=0.06,color_map='RGB',spin='both');
init_figure by using gridspec. gs = axs[0,0].get_gridspec() and then remove axes you want to replace for another shape, then add required axis by plt.gcf().add_subplot(gs[x_ind, y_ind]). This process is illustrated in below examples.import pivotpy.s_plots as sp
axs=sp.init_figure(figsize=(3.4,2.6),ncols=3,widths=[3.4,1,3.4],nrows=3,heights=[2.6,1,2.6],wspace=0.076,hspace=0.1)
[[sp.modify_axes(ax=a,xticks=[0],yticks=[0]) for a in ax] for ax in axs]
[sp.add_colorbar(ax=ax,ticks=[]) for ax in [axs[1,0],axs[1,2]]];
[sp.add_colorbar(ax=ax,vertical=True,ticks=[]) for ax in [axs[0,1],axs[2,1]]];
axs=sp.init_figure(figsize=(5,3.4),nrows=3,ncols=2,widths=[1,1],heights=[1,7,7],wspace=0.4,hspace=0.4,axes_off=[(2,0)],sharex=True,sharey=True)
import pivotpy.s_plots as sp
import matplotlib.pyplot as plt
gs = axs[0,0].get_gridspec()
axs_to_remove=[*axs[0, :],*axs[1:, 1]]
for ax in axs_to_remove:
ax.remove()
axlarge = plt.gcf().add_subplot(gs[0, :])
axv = plt.gcf().add_subplot(gs[1:, 1])
sp.modify_axes(ax=axv)
sp.add_colorbar(ax=axlarge)
sp.add_text(ax=axs[1,0],txts='axis[1,0]',xs=0.25,ys=0.5)
sp.add_text(ax=axs[2,0],txts='axis[2,0] is off',xs=0.15,ys=0.5)
import pivotpy.vr_parser as vp
vr=vp.export_vasprun(path='E:/Research/graphene_example/ISPIN_1/bands/vasprun.xml')
sp.quick_bplot(path_evr=vr,ax=axv,txt='Plotting',E_Fermi=10)
sp.add_text(ax=axv,txts='BigAxes',xs=0.25,ys=0.5)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pivotpy as pp
ax = pp.init_figure(nrows=2,ncols=2,widths=[1,2],heights=[1,2])
pos = ax[1,1].get_position()
ax[1,1].remove()
ax[1,1]=plt.gcf().add_axes(pos,projection='3d')
collect_dos is important, it will automatically return spin up/down saymbols.quick_dos_lines(path_evr='E:/Research/graphene_example/ISPIN_2/dos/vasprun.xml',vertical=False,fill_area=True,showlegend=True,include_dos='pdos',orbs=[[1,2,3],0,1],elements=[0,0,1],linewidth=1,labels=['p','s','g'],color_map='RGB',elim=[-5,5],spin='both')
The function below plt_to_html is implemented for use in pivotpy-dash app to view and save SVG image directly from web app's interface. This also enables high display output in jupyter notebook.
import pivotpy as pp
import matplotlib.pyplot as plt
pp.quick_bplot("E:/Research/graphene_example/ISPIN_1/bands/vasprun.xml",elim=[-9,9]);
fig = plt_to_html(dash_html=None,transparent=False)
fig
Below code snippest could be used to inclue svg in html document from a figure.
data = plt_to_html(dash_html=False)
html_str= """
<!DOCTYPE html>
<head></head>
<body>
<div>
{}
</div>
</body>
""".format(data)
with open('fig.html','w') as f:
f.write(html_str)
f.close()